package com.erik.lin.controller;

import com.erik.lin.annotion.MapResult;
import com.erik.lin.domain.Animal;
import com.erik.lin.domain.Person;
import com.erik.lin.redis.RedisSender;
import com.erik.lin.service.ApiService;
import com.erik.lin.service.MenuService;
import com.erik.lin.util.AppContext;
import com.erik.lin.util.Sender;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/test")
public class TestController {
	private Logger LOG = LoggerFactory.getLogger(TestController.class);

	@Autowired
	private JdbcTemplate jt;
	@Autowired
	private ApiService apiService;
	@Autowired
	private MenuService menuService;

	@RequestMapping(value = "/cache/api/{apiCode}", method = RequestMethod.GET)
	@Cacheable(value={"api"})
	public @ResponseBody Map<String, Object> getApiByApiCode(@PathVariable("apiCode") String apiCode) throws Exception{
//		Context ctx=new InitialContext();
//      DataSource= (DataSource)ctx.lookup("java:comp/env/jdbc/my_dataex");
//		JdbcTemplate jt = new JdbcTemplate(ds);
		String sql = "select * from dacp_dataex_api where api_code ='"+ apiCode +"'";
		Map<String, Object> apis = jt.queryForMap(sql);
		LOG.info("不执行缓存");
		return apis;
	}

	@RequestMapping(value = "/cache/api/removeAll", method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> save() {
		return apiService.removeAll();
	}

	@MapResult
	@RequestMapping(value = "/mq/test", method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> testMq() {
		Map rs = new HashMap();
		Sender<Person> sender = (Sender<Person>) AppContext.getBean("sender");
		sender.sendMessage("rec.person", new Person(25, "m", "Erik"));
		return rs;
	}

	@MapResult
	@RequestMapping(value = "/redisMq/test", method = RequestMethod.GET)
	public @ResponseBody Map<String, Object> testRedisMq() {
		Map rs = new HashMap();
		RedisSender.send("normalTopic", new Animal(25, "Erik Stark"));
		rs.put("success", true);
		return rs;
	}

	@RequestMapping(value = "/myJt/{dsName}/{tableName}", method = RequestMethod.GET)
	public @ResponseBody Object testMyJt(@PathVariable("dsName") String dsName, @PathVariable("tableName") String tableName) {
		com.erik.lin.util.JdbcTemplate myJt = new com.erik.lin.util.JdbcTemplate(dsName);
		String sql = "select * from "+tableName;
		return myJt.queryForList(sql);
	}

	@RequestMapping(value = "/menu/test", method = RequestMethod.GET)
	public @ResponseBody Object testNode() {
		return menuService.getMenuNode();
	}

	@RequestMapping(value = "/menu/test/some", method = RequestMethod.GET)
	public @ResponseBody Object testSomeNode() {
		List<String> menuIds = new ArrayList<>();
		menuIds.add("dev-team-member");
		menuIds.add("data-mgr-devq-question");
		return menuService.getSomeNode(menuIds);
	}

	public static void main(String[] args) {
		System.out.println(get());
	}


	public static int get() {
		int rs = 0;
		try {
			System.out.println("--------------------");
			return rs;
		}finally {
			rs += 1;
		}
	}


}
